<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加博客</title>

    <!-- 引入自己写的样式 -->
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/blog_edit.css">

    <!-- 引入 editor.md 的依赖 -->
    <link rel="stylesheet" href="editor.md/css/editormd.min.css" />
    <script src="js/jquery.min.js"></script>
    <script src="editor.md/editormd.js"></script>
    <script src="js/common.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/system.png" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="myblog_list.html">博客列表</a>
        <a href="person.html">资料修改</a>
        <a href="javascript: logout()">退出登录</a>
    </div>
    <!-- 编辑框容器 -->
    <div class="blog-edit-container">
        <!-- 标题编辑区 -->
        <div class="title">
            <input type="text" id="title" placeholder="在这里写下文章标题"><label for="title">&nbsp&nbsp</label>
            <button onclick="draft()">保存草稿</button>&nbsp&nbsp
            <button onclick="time()">定时发布</button>&nbsp&nbsp
            <label for="releasetime">&nbsp&nbsp</label><input type="text" id="releasetime" style="width: 420px;" placeholder="格式如:2/30表示两小时三十分钟后发布">&nbsp&nbsp
            <button onclick="mysub()">发布文章</button>
        </div>
        <!-- 创建编辑器标签 -->
        <div id="editorDiv">
            <label for="editor-markdown"></label><textarea id="editor-markdown" style="display:none;"></textarea>
        </div>
    </div>

    <script>
        let editor;

        function initEdit(md){
            // 编辑器设置
            editor = editormd("editorDiv", {
                // 这里的尺寸必须在这里设置. 设置样式会被 editormd 自动覆盖掉. 
                width: "100%",
                // 高度 100% 意思是和父元素一样高. 要在父元素的基础上去掉标题编辑区的高度
                height: "calc(100% - 50px)",
                // 编辑器中的初始内容
                markdown: md,
                // 指定 editor.md 依赖的插件路径
                path: "editor.md/lib/",
                saveHTMLToTextarea: true // 
            });
        }
        initEdit("# 在这里写下一篇博客"); // 初始化编译器的值

        function time() {

            if (confirm("是否定时发布？")) {
                let releasetime = jQuery("#releasetime");
                let title = jQuery("#title");

                if (title.val() === "") {
                    alert("请输入标题");
                    return;
                }   
                if (editor.getValue() === "") {
                    alert("请输入文章内容");
                    return;
                }
                if (releasetime.val() === "") {
                    alert("请输入定时发布时间");
                    return;
                }

                jQuery.ajax({
                    url: '/art/releasetime',
                    type: 'post',
                    data: {"title":  title.val(), "content": editor.getValue(), "releaseTime": releasetime.val()},
                    success: function(result) {
                        if (result != null && result.statusCode === 200 && result.data === 1) {
                            alert("定时发布成攻");
                            location.href = "blog_time.html";
                        } else {
                            alert("定时发布失败，请重试！");
                        }
                    }
                });
            }
        }

        function draft() {

            if (confirm("是否保存为草稿？")) {
                let title = jQuery("#title");

                jQuery.ajax({
                    url: '/art/draft',
                    type: 'post',
                    data: {"title": title.val(), "content": editor.getValue()},
                    success: function(result) {
                        if (result != null && result.statusCode === 200 && result.data === 1) {
                            alert("保存成功！");
                            location.href = "draftblog.html";
                        } else {
                            alert("保存失败，请重试");
                        }
                    }
                });
            }
        }

        // 提交
        function mysub(){
            // alert(editor.getValue()); // 获取值
            // editor.setValue("#123") // 设置值
            if (confirm("确认提交？")) {
                let title = jQuery("#title");
                if (title.val() === "") {
                    alert("请输入标题");
                    return;
                }
                if (editor.getValue() === "") {
                    alert("请输入文章正文");
                    return;
                }
                jQuery.ajax({
                    url: '/art/add',
                    type: 'post',
                    data: {"title": title.val(), "content": editor.getValue()},
                    success: function(result) {
                        if (result != null && result.statusCode === 200 && result.data === 1) {
                            if (confirm("添加成功，是否继续添加？")) {
                                location.href = location.href;
                            } else {
                                location.href = "/myblog_list.html";
                            }
                        } else {
                            alert("添加失败，请重试")
                        }
                    }
                });
            }
        }
    </script>
</body>

</html>